package com.server;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.Result;
import com.dto.SignUpDTO;
import com.utils.RegexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.beans.Account;
import com.mapper.AccountMapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service
public class AccountService extends ServiceImpl<AccountMapper , Account> implements IAccountService{
    @Autowired
    private AccountMapper accountMapper; // 注意这里的AccountMapper

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //根据id查询
    public Account getAccountById(Long id) {
        return accountMapper.selectById(id);
    }

    //查询所有
    public List<Account> getAllAccounts() {
        return accountMapper.selectList(null);
    }
    //添加用户
    public int createAccount(Account account) {
        return accountMapper.insert(account);
    }
    //修改用户信息
    public int updateAccount(Account account) {
        return accountMapper.updateById(account);
    }
    //删除用户
    public int deleteAccountById(Long id) {
        return accountMapper.deleteById(id);
    }

    public Result sendCode(String phone , HttpSession session) {

        //1.效验手机号
        if ( RegexUtils.isPhoneInvalid(phone) ) {
            return Result.fail("手机号格式输入错误!请重新输入");
        }

        //随机生成六位数字长度验证码
        String code = RandomUtil.randomNumbers(6);

        //将验证码存入Redis 过期时间设置为2分钟
        stringRedisTemplate.opsForValue().set(phone , code , 2 , TimeUnit.MINUTES);

        //模拟阿里云发送验证码
        log.debug("验证码发送成功：{}");

        String cacheCode = stringRedisTemplate.opsForValue().get(phone);

        return Result.ok("发送验证码成功，验证码为：" + cacheCode);
    }

    @Override
    public Result signUp(String phone , String code , String password
            , String passwords , HttpSession session) {

        /*String password = signUpDTO.getPassword();
        String passwords = signUpDTO.getPasswords();*/

        //再次效验手机号，防止注册时更改原手机号
        //String phone = signUpDTO.getPhone();

        if ( RegexUtils.isPhoneInvalid(phone) ) {
            return Result.fail("手机号格式错误！");
        }

        //效验验证码
        String cacheCode = stringRedisTemplate.opsForValue().get(phone);
        //String code = signUpDTO.getCode();

        //验证码不一致，返回错误信息
        if ( cacheCode == null || !cacheCode.equals(code) ) {
            return Result.fail("验证码错误！请重新输入正确验证码！");
        }

        //一致,匹配数据库中是否已经存在该用户
        Account account = query().eq("username", phone).one();

        //如果不存在，创建
        if( account == null ) {

            createUser(phone , password , passwords);

        }

        return Result.ok("注册成功！");
    }

    //创建新用户
    public void createUser(String phone , String password , String passwords) {

        Account account = new Account();

        //初始用户名以及密码均为电话号
        account.setUsername(phone);

        if( !password .equals(passwords) ) {
        }
        account.setPassword(password);

        save(account);
    }
}